<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle">
      <span class="placeholder">{{ cityStr }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="isShow">
      <div class="loading" v-if="loading"></div>
      <template v-else>
        <span
          class="ellipsis"
          v-for="i in city"
          :key="i.code"
          @click="cityClick(i)"
          >{{ i.name }}</span
        >
      </template>
    </div>
  </div>
</template>
<script>
import { reactive, ref } from 'vue'
import { getCityInfo } from '../../api/product'
import { onClickOutside } from '@vueuse/core'
export default {
  name: 'XtxCity',
  props: {
    cityStr: {
      type: String,
      default: '请选择配送地址'
    }
  },
  setup (props, { emit }) {
    const city = ref([])
    const initcity = ref([])
    const target = ref(null)
    const isShow = ref(false)
    const loading = ref(false)

    // 点击组件外关闭
    onClickOutside(target, event => close())
    // 点击组件外关闭原生做法
    // onMounted(() => {
    //   document.addEventListener('click', documentClickEvent)
    // })
    // onBeforeUnmount(() => {
    //   document.removeEventListener('click', documentClickEvent)
    // })
    // function documentClickEvent (e) {
    //   console.log(target.value)
    //   if (!target.value.contains(e.target)) {
    //     close()
    //   }
    //   // if (target.value.closest(e.target)) {
    //   //   console.log(1)
    //   // }
    // }

    //
    function toggle () {
      // console.log(1)
      isShow.value ? close() : open()
    }
    // 打开城市列表
    function open () {
      isShow.value = true
      if (city.value.length > 0) {
        // console.log(11)
        return
      }
      loading.value = true

      getCityInfo().then(res => {
        // console.log(res)
        city.value = res
        initcity.value = res
        loading.value = false
      })
    }
    // 关闭城市列表
    function close () {
      isShow.value = false
      // city.value = []
      city.value = initcity.value
    }
    // 城市选择
    const cityInfo = reactive({
      provinceName: '',
      provinceCode: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      cityStr: ''
    })
    function cityClick (item) {
      // console.log(item)
      if (item.level === 0) {
        city.value = item.areaList
        cityInfo.provinceName = item.name
        cityInfo.provinceCode = item.code
      } else if (item.level === 1) {
        city.value = item.areaList
        cityInfo.cityCode = item.code
        cityInfo.cityName = item.name
      } else {
        cityInfo.countyName = item.name
        cityInfo.countyCode = item.code
        cityInfo.cityStr =
          cityInfo.provinceName +
          ' ' +
          cityInfo.cityName +
          ' ' +
          cityInfo.countyName
        emit('change', cityInfo.cityStr)
        close()
      }
    }
    return {
      toggle,
      city,
      isShow,
      target,
      cityClick,
      loading
    }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
